package com.aplink.generic.pojo;

import java.util.ArrayList;
import java.util.HashMap;

public class ObjPropCol extends ArrayList<ObjPropInfo> {

    private static final long serialVersionUID = 1L;
    private ObjPropInfo io_keyProp;
    private ArrayList<String> ial_keys = new ArrayList<String>();
    private ArrayList<ObjPropInfo> ial_objects = new ArrayList<ObjPropInfo>();
    private JoinCol joins;
    private HashMap<String, ObjPropCol> childObjProps = new HashMap<String, ObjPropCol>();
    private ArrayList<String> ial_keys2 = new ArrayList<String>();
    private String syncid = "";
    private String mWebserviceFunction;

    public ObjPropCol() {
        mColumnAsc = new ArrayList<String>();
        mColumnDesc = new ArrayList<String>();
    }

    public void insert( String key, ObjPropInfo value ) {
        if ( ial_keys.contains( key ) == false ) {
            ial_keys.add( key );
            ial_objects.add( value );
            ial_keys2.add( ( value.getObjectMapping() + "." + value
                    .getObjectProperty() ).toLowerCase() );
            if ( value.getPrimaryObjectMapping() != null ) {
                ial_keys2.add( ( value.getPrimaryObjectMapping() + "." + value
                        .getObjectProperty() ).toLowerCase() );
            }
            this.add( value );
        }
    }

    public ArrayList<ObjPropInfo> GetBaseList() {
        return this.ial_objects;
    }

    public boolean containsKey( String key ) {
        return ial_keys.contains( key );
    }

    public boolean containsKey2( String AttributeId, String table ) {
        String key;
        key = ( table + "." + AttributeId ).toLowerCase();
        return ial_keys2.contains( key );
    }

    public ObjPropInfo getByKey( String key ) {
        if ( ial_keys.contains( key ) ) {
            Integer index = - 1;
            index = ial_keys.indexOf( key );
            if ( ial_objects.size() > index ) {
                return ial_objects.get( index );
            }
        }
        return null;
    }

    public ObjPropInfo get( Integer index ) {
        if ( ial_objects.size() > index ) {
            return ial_objects.get( index );
        }
        return null;
    }

    public String getSyncID() {
        return this.syncid;
    }

    public void set( String key, ObjPropInfo _objprop ) {
        Integer index = - 1;
        index = ial_keys.indexOf( key );
        if ( index > - 1 ) {
            this.set( index, _objprop );
            ial_objects.set( index, _objprop );
        }
    }

    public boolean IsKeyProp( String key ) {
        if ( io_keyProp != null ) {
            if ( io_keyProp.getObjectProperty().compareToIgnoreCase( key ) == 0 ) {
                return true;
            }
        }
        return false;
    }

    public ObjPropInfo getKeyProp() {
        return this.io_keyProp;
    }

    ;

    public void setKeyProp( ObjPropInfo _keyprop ) {
        this.io_keyProp = _keyprop;
    }

    public JoinCol getJoins() {
        return this.joins;
    }

    ;

    public void setJoins( JoinCol _joins ) {
        this.joins = _joins;
    }

    ;

    public HashMap<String, ObjPropCol> getChildObjProps() {
        return this.childObjProps;
    }

    ;

    public void setChildObjProps( HashMap<String, ObjPropCol> _childobjprops ) {
        this.childObjProps = _childobjprops;
    }

    ;

    public void setSyncID( String _syncid ) {
        this.syncid = _syncid;
    }

    public ObjPropInfo getPrimaryKeyObject() {
        for ( ObjPropInfo info : this ) {
            if ( info.isKey() ) {
                return info;
            }
        }
        return null;
    }

    public String getWebserviceFunction() {
        return mWebserviceFunction;
    }

    public void setWebserviceFunction( String webserviceFunction ) {
        mWebserviceFunction = webserviceFunction;
    }

    public void addSortAscending( String table, String column ) {
        mColumnAsc.add( String.format( "%s.%s", table, column ) );
    }

    public void addSortDecending( String table, String column ) {
        mColumnDesc.add( String.format( "%s.%s", table, column ) );
    }

    private ArrayList<String> mColumnAsc;
    private ArrayList<String> mColumnDesc;

    public String getOderByClause() {
        StringBuilder builder = new StringBuilder();
        if ( ! mColumnAsc.isEmpty() ) {
            for ( int i = 0 ; i < mColumnAsc.size() ; i++ ) {
                builder.append( mColumnAsc.get( i ) );
                if ( i < mColumnAsc.size() - 1 ) {
                    builder.append( "," );
                }
            }
            builder.append( " ASC " );
        }

        if ( ! mColumnDesc.isEmpty() ) {
            if ( ! mColumnAsc.isEmpty() ) {
                builder.append( "," );
            }

            for ( int i = 0 ; i < mColumnDesc.size() ; i++ ) {
                builder.append( mColumnDesc.get( i ) );
                if ( i < mColumnDesc.size() - 1 ) {
                    builder.append( "," );
                }
            }
            builder.append( " DESC " );
        }
        return builder.toString().isEmpty() ? null : builder.toString();
    }
}
